package com.cskaoyan.controller.user;

import com.cskaoyan.bean.MallUser;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.user.*;
import com.cskaoyan.service.user.UserManageService;
import com.cskaoyan.utils.RegularUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @program: project-market
 * @description:登录登出,首页,用户管理
 * @author: zhijie
 * @create: 2021-11-29 21:57
 **/
@RestController
@RequestMapping("admin")
public class UserManageController {
    @Autowired
    UserManageService userManageService;

    /**
     * 根据输入的条件模糊查询用户的数量
     *
     * @param username
     * @param mobile
     * @param userId
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:user:list")
    @GetMapping("user/list")
    public BaseRespVo userList(String username, String mobile, String userId, PageBo pageBo) {
        if (mobile != null && !"".equals(mobile)) {
            if (RegularUtil.isNumber(mobile) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        if (userId != null && !"".equals(userId)) {
            if (RegularUtil.isNumber(userId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        UserListData data = userManageService.userList(username, mobile, userId, pageBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:user:detail")
    @GetMapping("user/detail")
    public BaseRespVo userDetail(String id) {
        if (id != null && !"".equals(id)) {
            if (RegularUtil.isNumber(id) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        MallUser data = userManageService.userDetail(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 根据条件查询用户的信息
     *
     * @param userId
     * @param name
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:address:list")
    @GetMapping("address/list")
    public BaseRespVo addressList(String userId, String name, PageBo pageBo) {
        if (userId != null && !"".equals(userId)) {
            if (RegularUtil.isNumber(userId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        AddressListVo data = userManageService.addressList(userId, name, pageBo);

        return BaseRespVo.ok(data);
    }

    /**
     * 模糊查询用户的收藏列表
     *
     * @param userId
     * @param valueId
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:collect:list")
    @GetMapping("collect/list")
    public BaseRespVo collectList(String userId, String valueId, PageBo pageBo) {
        if (userId != null && !"".equals(userId)) {
            if (RegularUtil.isNumber(userId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        if (valueId != null && !"".equals(valueId)) {
            if (RegularUtil.isNumber(valueId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        CollectListVO data = userManageService.collectList(userId, valueId, pageBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 模糊查询用户的足迹
     *
     * @param userId
     * @param goodsId
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:footprint:list")
    @GetMapping("footprint/list")
    public BaseRespVo footprintList(String userId, String goodsId, PageBo pageBo) {
        if (userId != null && !"".equals(userId)) {
            if (RegularUtil.isNumber(userId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        if (goodsId != null && !"".equals(goodsId)) {
            if (RegularUtil.isNumber(goodsId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        FootPrintVo data = userManageService.footprintList(userId, goodsId, pageBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 根据userId和keyword查询历史记录
     *
     * @param userId
     * @param keyword
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:history:list")
    @GetMapping("history/list")
    public BaseRespVo historyList(String userId, String keyword, PageBo pageBo) {
        if (userId != null && !"".equals(userId)) {
            if (RegularUtil.isNumber(userId) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        HistoryListVo data = userManageService.historyList(userId, keyword, pageBo);
        Date date = new Date();
        return BaseRespVo.ok(data);
    }

    /**
     * 根据反馈id和用户查询反馈信息
     * @param id
     * @param username
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:feedback:list")
    @GetMapping("feedback/list")
    public BaseRespVo feedbackList(String id, String username, PageBo pageBo) {
        if (id != null && !"".equals(id)) {
            if (RegularUtil.isNumber(id) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        FeedbackListVo data = userManageService.feedbackList(id, username, pageBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 只更改用户的等级跟状态,其余的都不更新
     * @return
     */
    @RequiresPermissions("admin:user:update")
    @PostMapping("user/update")
    public BaseRespVo userUpdate(@RequestBody Map<String,String>map){
        Integer id = Integer.parseInt(map.get("id")) ;
        Byte status = Byte.parseByte(map.get("status")) ;
        Byte userLevel = Byte.parseByte(map.get("userLevel")) ;
       int row=  userManageService.userUpdate(status,userLevel,id);
       if (row==1){
           return BaseRespVo.manual(0,"成功",1);
       }
       return BaseRespVo.fail("参数有误");

    }

}
